package com.example.fish.controller;

import com.example.fish.pojo.Comment;
import com.example.fish.pojo.User;
import com.example.fish.pojo.UserComment;
import com.example.fish.service.CommentService;
import com.example.fish.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController{
    @Autowired
    private CommentService commentService;

    @PostMapping("/list")
    public JsonResult<List<Comment>> getCommentList(Integer blogid){
        List<Comment> comments = commentService.getCommentByBlogid(blogid);
        return new JsonResult<>(200,"success",comments);
    }

    @PostMapping("/publish")
    public JsonResult<Void> publishComment(Integer blogid, String content, HttpSession session){
//        得到userid
        User user = (User) session.getAttribute("user");
        Integer userid = user.getUserid();
        commentService.saveCommentOne(userid,blogid,content);
        return new JsonResult<>(200,"success",null);
    }


    @PostMapping("/publishTwo")
    public JsonResult<Void> publishCommentTwo(HttpSession session,Integer targetUserid,Integer whichCommentid,String content,Integer blogid){
        User user = (User) session.getAttribute("user");
        Integer userid = user.getUserid();
        commentService.saveCommentTwo(userid,targetUserid,whichCommentid,content,blogid);
        return new JsonResult<>(200,"success",null);
    }

    @PostMapping("/detailComment")
    public JsonResult<PageInfo<UserComment>> getUserComment(Integer pageNum,HttpSession session){
        Integer pageSize = 5;
        User user =(User) session.getAttribute("user");
        PageInfo<UserComment> pageInfo = commentService.getCommentOneByUserid(pageNum, pageSize, user.getUserid());
        return new JsonResult<>(200,"success",pageInfo);
    }
    @PostMapping("/deleteByCommentId")
    public JsonResult<Void> deleteCommentByCommentId(Integer commentid){
        commentService.deleteCommentByCommentid(commentid);
        return new JsonResult<>(200,"success",null);
    }
}
